//
//  SampuxEquipmentDetailView.m
//  Sampux
//
//  Created by rzm on 16/7/6.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxEquipmentDetailView.h"
#import "SampuxAttributedString.h"
#import "SampuxDetailContentView.h"
#import "SampuxGlobalTool.h"
#import "SmapuxSettingUnitButton.h"
#import "SampuxModelSegView.h"
#import "UIScrollView+MJRefresh.h"
#import "SampuxCustomSlider.h"
#import "SampuxGetCurrentTime.h"

@interface SampuxEquipmentDetailView ()
{
    UIButton *_selectedInstallBtn;
    UIButton *_selectedWorkModelBtn;

}
@property (nonatomic, strong) NSMutableArray *installBtnArr ;
@property (nonatomic, strong) UIView *modeSettingView ;
@property (strong,nonatomic) NSMutableArray *segBtnArray ;
@property (nonatomic, strong) UIImageView *btnBackimgView ;
@property (nonatomic, strong) UIView *modeView ;
@property (nonatomic, strong) UILabel *showTempLab;
@property (nonatomic, strong) UIImageView *imgView ;
@property (nonatomic, strong) UIView *btnBackView ;
@property (nonatomic, strong) UISlider *tempSlider ;
@property (nonatomic, strong) SampuxDetailContentView *tempView ;
@property (nonatomic, strong) SampuxDetailContentView *humidityView ;
@property (nonatomic, copy) NSString *gaugingTemp;
@property (nonatomic, strong) NSMutableArray *toolBarArr;
@property (nonatomic, copy) NSString *WaterTankTemp;
@property (nonatomic, copy) NSString *beSettedTemp;

@property (nonatomic, strong) UIView *stateOnView;
@property (nonatomic, strong) UIView *stateOffView;

@property (nonatomic, strong) SmapuxSettingUnitButton *antifreezingBtn;
@property (nonatomic, strong) SmapuxSettingUnitButton *heatStateBtn;

@property (nonatomic, strong) UIButton *segLeftBtn ;
@property (nonatomic, strong) UIButton *segRightBtn ;
//@property (nonatomic, strong) SampuxContentBtn *shutBtnOnHand ;
//@property (nonatomic, strong) SampuxContentBtn *shutBtnOnTime ;
@property (nonatomic, strong) SampuxContentBtn *heatModeBtnOnTime ;
@property (nonatomic, strong) UIView *modeLineView;
//@property (nonatomic, strong) UILabel *titleLab ;

@property (nonatomic, strong) UIView *onTimebackView;
@property(nonatomic,assign) BOOL isConnectOrNot;


@end

@implementation SampuxEquipmentDetailView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _installBtnArr = [NSMutableArray array];
        _segBtnArray = [NSMutableArray array];
        _toolBarArr = [NSMutableArray array];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestSwitch:) name:NOTIFICATION_RESPONSE_SET_POWERSWITCH object:nil];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestWorkMode:) name:NOTIFICATION_RESPONSE_SET_WORKMODE object:nil];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestHeatMode:) name:NOTIFICATION_RESPONSE_SET_HEATMODE object:nil];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setTemp:) name:NOTIFICATION_RESPONSE_SET_TEMPE object:nil];
        self.isConnectOrNot = YES;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(connectFail) name:@"socketConnectFail" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectSuccess) name:@"socketConnectSuccess" object:nil];
        [self setupSubView];
        [self layoutSubviews];
    }
    return self;
}

- (void)connectSuccess
{
    self.isConnectOrNot = YES;
}

- (void)connectFail
{
    self.isConnectOrNot = NO;
}

- (void)setupSubView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 16.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 16.;
    }else if (IS_IPHONE_6)
    {
        font = 18.;
    }else
    {
        font = 18.;
    }

    UIScrollView *backView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.width, self.height - 64)];
    [self addSubview:backView];
    backView.userInteractionEnabled = YES;
    backView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight);
    
    _header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    backView.mj_header = _header;
//    [_header beginRefreshing];
    
    _modeSettingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, backView.width,Sampux6HScale(50))];
    [backView addSubview:_modeSettingView];
    [self createmodeSettingView];

    _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.width/2 -  Sampux6WScale(652/2)/2, _modeSettingView.bottom + Sampux6HScale(20), Sampux6WScale(652/2), Sampux6HScale(493/2))];
    _imgView.image = imageNamed(@"house");
    _imgView.userInteractionEnabled = YES;
    [backView addSubview:_imgView];
    [self createImgDetailView];
    
    _stateOnView = [[UIView alloc]initWithFrame:CGRectMake(0, _imgView.bottom + Sampux6HScale(20), backView.width, self.height - _imgView.bottom - Sampux6HScale(20))];
    [backView addSubview:_stateOnView];
//    _stateOnView.backgroundColor = RandomColor;
    [self createOnLineView];
}

- (void)refreshData
{
    if (self.model != nil) {
        [GBInterMaster_CommForDeivce requestDeviceStatus:0 withDeviceAccount:self.model.accountStr];
    } else {
        [_header endRefreshing];
    }
}

- (void)setModel:(SampuxListModel *)model
{
    [_header endRefreshing];
    _model = model;
    _tempSlider.minimumValue = _model.tempSetLimitLown;
    _tempSlider.maximumValue = _model.tempSetLimitTop;

//    _selectedInstallBtn = nil;
    [self getDataSource:_model];

//    self.isTimerModeOrNot = _model.isTimeMode;
    UIButton *timeBtn = self.segBtnArray[0];
    UIButton *handBtn = self.segBtnArray[1];

    if (_model.isShutDownOrNot == NO)
    {
        self.shutBtnOnHand.selected = NO;
        self.shutBtnOnTime.selected = NO;
    } else
    {
        self.shutBtnOnHand.selected = YES;
        self.shutBtnOnTime.selected = YES;
    }
    
    if (_model.isTimeMode == YES) {
        timeBtn.backgroundColor = THBlackAlplaColor(0.12);
        handBtn.backgroundColor = THBlackAlplaColor(0.08);
        [timeBtn setTitleColor:THDeFaultBlueColor forState:UIControlStateNormal];
        [handBtn setTitleColor:THWhiteAlplaColor(0.6) forState:UIControlStateNormal];
        timeBtn.userInteractionEnabled = NO;
        handBtn.userInteractionEnabled = YES;
        _modeLineView.x = 0;
        _btnBackView.hidden = YES;
        _onTimebackView.hidden = NO;
        _showTempLab.text = [NSString stringWithFormat:@"设定温度 %.f℃",_model.currentSetTempe];
        _tempSlider.value = _model.ProgramSetTemp;
        
    }else
    {
        timeBtn.backgroundColor = THBlackAlplaColor(0.08);
        handBtn.backgroundColor = THBlackAlplaColor(0.12);
        [timeBtn setTitleColor:THWhiteAlplaColor(0.6) forState:UIControlStateNormal];
        [handBtn setTitleColor:THDeFaultBlueColor forState:UIControlStateNormal];
        timeBtn.userInteractionEnabled = YES;
        handBtn.userInteractionEnabled = NO;
        _modeLineView.x = self.modeSettingView.width/2;
        _btnBackView.hidden = NO;
        _onTimebackView.hidden = YES;
        
        if (_model.heatMode == 1) {
            _tempSlider.value = _model.QKHSetTemp;
            _selectedInstallBtn = self.installBtnArr[0];
            _showTempLab.text = [NSString stringWithFormat:@"设定温度 %.f℃",_model.QKHSetTemp];
        }else if (_model.heatMode == 2)
        {
            _tempSlider.value = _model.CFTSetTemp;
            _selectedInstallBtn = self.installBtnArr[1];
            _showTempLab.text = [NSString stringWithFormat:@"设定温度 %.f℃",_model.CFTSetTemp];
        }else
        {
            _tempSlider.value = _model.ECSetTemp;
            _showTempLab.text = [NSString stringWithFormat:@"设定温度 %.f℃",_model.ECSetTemp];
            _selectedInstallBtn = self.installBtnArr[2];
        }
        [self.installBtnArr enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj == _selectedInstallBtn) {
                obj.selected = YES;
                obj.userInteractionEnabled = NO;
            }else
            {
                obj.selected = NO;
                obj.userInteractionEnabled = YES;
            }
        }];
    }
    
    if (_model.heatMode == 1) {
        [_heatModeBtnOnTime setImage:[UIImage imageNamed:@"fasthot_selected"] forState:UIControlStateNormal];
        [_heatModeBtnOnTime setTitle:@"速热" forState:UIControlStateNormal];
    }else if (_model.heatMode == 2)
    {
        [_heatModeBtnOnTime setImage:[UIImage imageNamed:@"comfortable_selected"] forState:UIControlStateNormal];
        [_heatModeBtnOnTime setTitle:@"舒适" forState:UIControlStateNormal];
    }else
    {
        [_heatModeBtnOnTime setImage:[UIImage imageNamed:@"efficient_selected"] forState:UIControlStateNormal];
        [_heatModeBtnOnTime setTitle:@"节能" forState:UIControlStateNormal];
    }

    [_heatStateBtn setTitle:@"加热状态" forState:UIControlStateNormal];
    if (_model.isHeat) {
        self.heatStateBtn.selected = YES;
    }else
    {
        self.heatStateBtn.selected = NO;
    }
//    
 
}

//- (void)setIsTimerModeOrNot:(BOOL)isTimerModeOrNot
//{
//    _isTimerModeOrNot = isTimerModeOrNot;
//    UIButton *timeBtn = self.segBtnArray[0];
//    UIButton *handBtn = self.segBtnArray[1];
//    if (_isTimerModeOrNot) {
//        timeBtn.backgroundColor = THBlackAlplaColor(0.12);
//        handBtn.backgroundColor = THBlackAlplaColor(0.08);
//        [timeBtn setTitleColor:THDeFaultBlueColor forState:UIControlStateNormal];
//        [handBtn setTitleColor:THWhiteAlplaColor(0.6) forState:UIControlStateNormal];
//        timeBtn.userInteractionEnabled = NO;
//        handBtn.userInteractionEnabled = YES;
//        _modeLineView.x = 0;
//        _tempSlider.userInteractionEnabled = YES;
//        _tempSlider.value = _model.ProgramSetTemp;
//        _shutBtnOnHand.hidden = YES;
//        _btnBackView.hidden = YES;
//        _shutBtnOnTime.hidden = NO;
//        _heatModeBtnOnTime.hidden = NO;
//
//    }else
//    {
//        timeBtn.backgroundColor = THBlackAlplaColor(0.08);
//        handBtn.backgroundColor = THBlackAlplaColor(0.12);
//        [timeBtn setTitleColor:THWhiteAlplaColor(0.6) forState:UIControlStateNormal];
//        [handBtn setTitleColor:THDeFaultBlueColor forState:UIControlStateNormal];
//        timeBtn.userInteractionEnabled = YES;
//        handBtn.userInteractionEnabled = NO;
//        _tempSlider.userInteractionEnabled = YES;
//        _modeLineView.x = self.modeSettingView.width/2;
//        _shutBtnOnHand.hidden = NO;
//        _btnBackView.hidden = NO;
//        _shutBtnOnTime.hidden = YES;
//        _heatModeBtnOnTime.hidden = YES;
//    }
//}

- (void)createOnLineView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 18.;
    }else
    {
        font = 18.;
    }
    _showTempLab = [[UILabel alloc]initWithFrame:CGRectMake(self.width * 0.2, Sampux6HScale(5), _stateOnView.width *0.6, 30)];
    [_stateOnView addSubview:_showTempLab];
    _showTempLab.textAlignment = NSTextAlignmentCenter;
    _showTempLab.textColor = THWhiteAlplaColor(0.8);
    _showTempLab.font = THFont(font);
    
    UIImageView *tempImg = [[UIImageView alloc]initWithFrame:CGRectMake(1, _showTempLab.bottom + Sampux6HScale(25), _stateOnView.width - 1, Sampux6HScale(105/2))];
    [_stateOnView addSubview:tempImg];
    tempImg.userInteractionEnabled = YES;
    [tempImg setImage:imageNamed(@"dialgauge")];
    
    _tempSlider = [[UISlider alloc]initWithFrame:CGRectMake(Sampux6WScale(8),tempImg.height/2 - Sampux6HScale(33)/2, tempImg.width -Sampux6WScale(16), Sampux6HScale(33))];
    [tempImg addSubview:_tempSlider];

    [_tempSlider setThumbImage:imageNamed(@"slide8") forState:UIControlStateNormal];
    [_tempSlider setThumbImage:imageNamed(@"slide8") forState:UIControlStateHighlighted];
    [_tempSlider setMaximumTrackImage:imageNamed(@"minImg") forState:UIControlStateNormal];
    [_tempSlider setMinimumTrackImage:imageNamed(@"maxImg") forState:UIControlStateNormal];
    _tempSlider.continuous = NO;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTapGesture:)];
    [_tempSlider addGestureRecognizer:tapGesture];
    [_tempSlider addTarget:self action:@selector(tempSliderChange:) forControlEvents:UIControlEventTouchUpInside];

   _onTimebackView = [[UIView alloc]initWithFrame:CGRectMake(0, tempImg.bottom + Sampux6HScale(35), self.width, Sampux6HScale(75))];
    [_stateOnView addSubview:_onTimebackView];
    
    CGFloat space1 = (_onTimebackView.width - Sampux6WScale(96/2) *2)/3;
    _shutBtnOnTime = [[SampuxContentBtn alloc]initWithFrame:CGRectMake(space1, 0, Sampux6WScale(96/2), _onTimebackView.height)];
    [_onTimebackView addSubview:_shutBtnOnTime];
    [_shutBtnOnTime setImage:imageNamed(@"shut") forState:UIControlStateNormal];
    [_shutBtnOnTime setImage:imageNamed(@"shut_selected") forState:UIControlStateSelected];
    [_shutBtnOnTime setTitle:@"开关" forState:UIControlStateNormal];
    [_shutBtnOnTime addTarget:self action:@selector(shutOnTimeClick:) forControlEvents:UIControlEventTouchUpInside];
    _shutBtnOnTime.titleLabel.font = THFont(font - 4);
//    _shutBtnOnTime.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10);

    _heatModeBtnOnTime = [[SampuxContentBtn alloc]initWithFrame:CGRectMake(_shutBtnOnTime.right +space1, 0, Sampux6WScale(96/2), _onTimebackView.height)];
    _heatModeBtnOnTime.userInteractionEnabled = NO;
        [_onTimebackView addSubview:_heatModeBtnOnTime];
    _heatModeBtnOnTime.titleLabel.font = THFont(font - 4);
//    _heatModeBtnOnTime.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10);

    CGFloat space = (self.width - Sampux6WScale(96/2) *4)/5;
//    _shutBtnOnHand = [[SampuxContentBtn alloc]initWithFrame:CGRectMake(space, 0, Sampux6WScale(96/2), backView.height)];
//    [backView addSubview:_shutBtnOnHand];
//    [_shutBtnOnHand setImage:imageNamed(@"shut") forState:UIControlStateNormal];
//    [_shutBtnOnHand setImage:imageNamed(@"shut_selected") forState:UIControlStateSelected];
//    [_shutBtnOnHand setTitle:@"开关" forState:UIControlStateNormal];
//    [_shutBtnOnHand addTarget:self action:@selector(shutOnHandClick:) forControlEvents:UIControlEventTouchUpInside];
//    _shutBtnOnHand.titleLabel.font = THFont(font - 4);
//    _shutBtnOnHand.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10);

//    _btnBackView = [[UIView alloc]initWithFrame:CGRectMake(space*2 +Sampux6WScale(96/2),0, backView.width - (space +_shutBtnOnHand.right), backView.height)];
    _btnBackView = [[UIView alloc]initWithFrame:CGRectMake(0,tempImg.bottom + Sampux6HScale(35), self.width, Sampux6HScale(75))];
    [_stateOnView addSubview:_btnBackView];
//    _btnBackView.backgroundColor = RandomColor;
    
    _shutBtnOnHand = [[SampuxContentBtn alloc]initWithFrame:CGRectMake(space, 0, Sampux6WScale(96/2), Sampux6HScale(75))];
    [_btnBackView addSubview:_shutBtnOnHand];
    [_shutBtnOnHand addTarget:self action:@selector(shutBtnOnHandClick:) forControlEvents:UIControlEventTouchUpInside];
    [_shutBtnOnHand setImage:imageNamed(@"shut") forState:UIControlStateNormal];
    [_shutBtnOnHand setImage:imageNamed(@"shut_selected") forState:UIControlStateSelected];
    [_shutBtnOnHand setTitle:@"开关" forState:UIControlStateNormal];
    _shutBtnOnHand.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10);
    _shutBtnOnHand.titleLabel.font = THFont(font - 4);

    
    NSArray *imgArr = @[@"fasthot",@"comfortable",@"efficient"];
    NSArray *selectedImgArr = @[@"fasthot_selected",@"comfortable_selected",@"efficient_selected"];
    NSArray *btnArr = @[@"速热",@"舒适",@"节能"];
    for (int i = 0; i < 3; i ++) {
        SampuxContentBtn *btn = [[SampuxContentBtn alloc]initWithFrame:CGRectMake((i+1) *(Sampux6WScale(96/2) + space)+ space ,0, Sampux6WScale(96/2), _btnBackView.height)];
        [btn setImage:[UIImage imageNamed:imgArr[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectedImgArr[i]] forState:UIControlStateSelected];
        [_btnBackView addSubview:btn];
        btn.tag = 10000 + i;
        [_installBtnArr addObject:btn];
        [btn addTarget:self action:@selector(installBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = THFont(font - 4);
        [btn setTitle:btnArr[i] forState:UIControlStateNormal];
        btn.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10);
    }
}

- (void)actionTapGesture:(UITapGestureRecognizer *)sender {
    CGPoint touchPoint = [sender locationInView:_tempSlider];
    int value = (_tempSlider.maximumValue - _tempSlider.minimumValue) * (touchPoint.x / _tempSlider.frame.size.width) +_tempSlider.minimumValue;
    [_tempSlider setValue:value animated:YES];
    self.showTempLab.text = [NSString stringWithFormat:@"设置温度 %d℃",value];
    if (self.isConnectOrNot == YES) {
        [GBInterMaster_CommForDeivce requestSetTempe:0 withDeviceAccount:self.model.accountStr withHeatMode:self.model.heatMode withTempe:value];
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"网络未连接，请检查您的网络连接设置"];
    }
}
//- (void)createOffView
//{
//    CGFloat font = 8.;
//    if (IS_IPHONE_4_OR_LESS) {
//        font = 14.;
//    }else if (IS_IPHONE_5_OR_LESS)
//    {
//        font = 14.;
//    }else if (IS_IPHONE_6)
//    {
//        font = 16.;
//    }else
//    {
//        font = 16.;
//    }
//    
//    UIView *backView = [[UIView alloc]initWithFrame:_stateOffView.bounds];
//    backView.backgroundColor = THBlackAlplaColor(0.9);
//    [_stateOffView addSubview:backView];
//    backView.alpha = 1;

//    UIButton *backBtn = [UIButton createButton];
//    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    backBtn.frame = CGRectMake(10, 20, 40, 40);
//    [backView addSubview:backBtn];
//    [backBtn setImage:[UIImage imageNamed:@"arrow_alpha"] forState:UIControlStateNormal];
//    
//    _titleLab = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2 - Sampux6WScale(250)/2, 20, Sampux6WScale(250), 40)];
//    [backView addSubview:_titleLab];
//    _titleLab.textColor = THWhiteAlplaColor(0.7);
//    _titleLab.textAlignment = NSTextAlignmentCenter;
//    _titleLab.text = self.titleStr;
//    
//    UIButton *settingBtn = [UIButton createButton];
//    [backView addSubview:settingBtn];
//    [settingBtn setImage:imageNamed(@"setting") forState:UIControlStateNormal];
//    settingBtn.frame = CGRectMake(kScreenWidth - 10 - 40, 20, 40, 40);
//    [settingBtn addTarget:self action:@selector(settingBtnClick) forControlEvents:UIControlEventTouchUpInside];

//    UIButton *offBtnImg = [UIButton createButton];
//    offBtnImg.frame = CGRectMake(_stateOffView.width/2 - Sampux6HScale(118/2)/2, _stateOffView.height * 0.3, Sampux6HScale(118/2), Sampux6HScale(118/2));
//    [offBtnImg setBackgroundImage:imageNamed(@"shutdown") forState:UIControlStateNormal];
//    [offBtnImg addTarget:self action:@selector(shutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [backView addSubview:offBtnImg];
//
//    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(self.width/2 - 60, offBtnImg.y - 50, 120, 40)];
//    lab.textColor = THWhiteAlplaColor(0.6);
//    lab.text = @"此设备已关机";
//    lab.textAlignment = NSTextAlignmentCenter;
//    lab.font = THFont(font + 2);
//    [backView addSubview:lab];
//
//    _antifreezingBtn = [[SmapuxSettingUnitButton alloc]initWithFrame: CGRectMake(backView.width/2- Sampux6WScale(60)/2, offBtnImg.bottom + Sampux6HScale(30), Sampux6WScale(120/2), Sampux6WScale(120/2))];
//    _antifreezingBtn.titleLabel.font = THFont(font - 4);
//    _antifreezingBtn.userInteractionEnabled = NO;
//    [self.antifreezingBtn setTitle:@"防冻已开启" forState:UIControlStateNormal];
//    [self.antifreezingBtn setImage:imageNamed(@"antifreeze") forState:UIControlStateNormal];
//    [backView addSubview:_antifreezingBtn];
//}
//
- (void)createmodeSettingView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 16.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 16.;
    }else if (IS_IPHONE_6)
    {
        font = 18.;
    }else
    {
        font = 18.;
    }
    NSArray *arr = @[@"定时模式",@"手动模式"];
    for (int i = 0; i < 2; i ++) {
        UIButton *btn = [UIButton createButton];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [self.modeSettingView addSubview:btn];
        btn.frame = CGRectMake(self.modeSettingView.width/2*i, 0, self.modeSettingView.width/2, self.modeSettingView.height);
        btn.tag = i + 10;
        [btn setTitleColor:THWhiteAlplaColor(0.8) forState:UIControlStateNormal];
        btn.titleLabel.font = THFont(font);
        [btn addTarget:self action:@selector(clickSegmentButtonWithTag:) forControlEvents:UIControlEventTouchUpInside];
        [_segBtnArray addObject:btn];
    }
    _modeLineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.modeSettingView.height - 1, self.modeSettingView.width/2, 1)];
    _modeLineView.backgroundColor = THWhiteAlplaColor(0.3);
    [self.modeSettingView addSubview:_modeLineView];
}

- (void)createImgDetailView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 18.;
    }else
    {
        font = 18.;
    }
    [self createStateImgView];
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(Sampux6WScale(83/2), Sampux6HScale(95) , self.imgView.width - Sampux6WScale(83), self.imgView.height - Sampux6HScale(95))];
    [self.imgView addSubview:contentView];
    contentView.backgroundColor = THSystemClearColor;
    
    _tempView = [[SampuxDetailContentView alloc]initWithFrame:CGRectMake(Sampux6WScale(8), Sampux6HScale(10), contentView.width/2 - Sampux6WScale(8), contentView.height - Sampux6HScale(10))];
    [contentView addSubview:_tempView];
    [_tempView.contentBtn setTitle:@"室内温度" forState:UIControlStateNormal];
    _tempView.contentBtn.titleLabel.font = THFont(font);
    [_tempView.contentBtn setImage:imageNamed(@"tem") forState:UIControlStateNormal];
    [_tempView.contentBtn setTitleColor:THWhiteAlplaColor(0.64) forState:UIControlStateNormal];
    _tempView.dateLable.textAlignment = NSTextAlignmentCenter;

    _humidityView = [[SampuxDetailContentView alloc]initWithFrame:CGRectMake(_tempView.right, Sampux6HScale(10), contentView.width/2 - Sampux6WScale(8), contentView.height - Sampux6HScale(10))];
    [contentView addSubview:_humidityView];
    _humidityView.contentBtn.titleLabel.font = THFont(font);
    [_humidityView.contentBtn setTitle:@"室内湿度" forState:UIControlStateNormal];
    [_humidityView.contentBtn setImage:imageNamed(@"moisture") forState:UIControlStateNormal];
    [_humidityView.contentBtn setTitleColor:THWhiteAlplaColor(0.64) forState:UIControlStateNormal];
     _humidityView.dateLable.textAlignment = NSTextAlignmentCenter;

}

- (void)createStateImgView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 12.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 12.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 16.;
    }

    _heatStateBtn = [[SmapuxSettingUnitButton alloc]initWithFrame: CGRectMake(self.imgView.width/2- Sampux6HScale(145/2)/2, Sampux6HScale(30), Sampux6HScale(145/2), Sampux6HScale(130/2))];
    _heatStateBtn.titleLabel.font = THFont(font);
    _heatStateBtn.userInteractionEnabled = NO;
    [self.imgView addSubview:_heatStateBtn];
    [self.heatStateBtn setImage:imageNamed(@"heat_selected") forState:UIControlStateSelected];
    [self.heatStateBtn setImage:imageNamed(@"heat") forState:UIControlStateNormal];
}


//- (void)settingBtnClick
//{
//    if (self.offStateClickBlock) {
//        self.offStateClickBlock();
//    }
//}
//
//- (void)backBtnClick
//{
////    [self stateOffViewHide];
//    if (self.backBlock) {
//        self.backBlock();
//    }
//}

- (void)tempSliderChange:(UISlider *)slider
{
    slider.value = abs(slider.value);
    self.showTempLab.text = [NSString stringWithFormat:@"设置温度 %d℃",abs(slider.value)];
    
    if (self.isConnectOrNot == YES) {
        [GBInterMaster_CommForDeivce requestSetTempe:0 withDeviceAccount:self.model.accountStr withHeatMode:self.model.heatMode withTempe:abs(slider.value)];
    }
    self.model.currentSetTempeSetTime = [SampuxGetCurrentTime getCurrentTimeWithDate];
}

- (void)clickSegmentButtonWithTag:(UIButton *)button
{
    SampuxListModel *model = self.model;
    model.isTimeMode = !self.model.isTimeMode;
    self.model = model;
    self.model.workModeSetTime = [SampuxGetCurrentTime getCurrentTimeWithDate];

    if (self.model.isTimeMode) {
        if (self.isConnectOrNot == YES) {
            [GBInterMaster_CommForDeivce requestWorkMode:0 withDeviceAccount:self.model.accountStr withWorkMode:3];

        }
    }else
    {
        if (self.isConnectOrNot == YES) {
            [GBInterMaster_CommForDeivce requestWorkMode:0 withDeviceAccount:self.model.accountStr withWorkMode:1];
        }
    }
//    if (button.tag == 10) {
//        [GBInterMaster_CommForDeivce requestWorkMode:0 withDeviceAccount:self.model.accountStr withWorkMode:3];
//
//        self.isTimerModeOrNot = YES;
//    }else
//    {
//        [GBInterMaster_CommForDeivce requestWorkMode:0 withDeviceAccount:self.model.accountStr withWorkMode:1];
//
//         self.isTimerModeOrNot = NO;
//    }
}

- (void)getDataSource:(SampuxListModel*)model
{
    NSString *inStr = [NSString stringWithFormat:@"%.f",model.currentRoomTempe];
    NSString *outStr = [NSString stringWithFormat:@"%.f",model.currentRoomHumidity];

    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 65.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 65.;
    }else if (IS_IPHONE_6)
    {
        font = 70.;
    }else
    {
        font = 70.;
    }
    if (model.currentRoomTempe == 10000) {
        _tempView.numberStr = [NSString stringWithFormat:@"E2%@",THUnitType_C];
        NSDictionary *numDict = [NSDictionary dictionaryWithObjectsAndKeys:THFont(font),NSFontAttributeName,THSystemRedColor,NSForegroundColorAttributeName, nil];
        NSDictionary *unitDict = [NSDictionary dictionaryWithObjectsAndKeys:THFont(font - 53),NSFontAttributeName,THWhiteAlplaColor(0.6),NSForegroundColorAttributeName, nil];
        NSRange range = [_tempView.numberStr rangeOfString:THUnitType_C];
        NSMutableAttributedString *mutStr = [[NSMutableAttributedString alloc]initWithString:_tempView.numberStr attributes:numDict];
        [mutStr setAttributes:unitDict range:range];
        _tempView.dateLable.attributedText = mutStr;
    }else
    {
        _tempView.numberStr = [NSString stringWithFormat:@"%@%@",inStr,THUnitType_C];

        _tempView.dateLable.attributedText = [SampuxAttributedString handleUnitString:_tempView.numberStr unitFont:font - 53 numFont:font unitString:THUnitType_C];
    }
      if (model.currentRoomHumidity == 10000) {
          
          _humidityView.numberStr  = [NSString stringWithFormat:@"E1%@",THUnitType_percent];
          NSDictionary *numDict = [NSDictionary dictionaryWithObjectsAndKeys:THFont(font),NSFontAttributeName,THSystemRedColor,NSForegroundColorAttributeName, nil];
          NSDictionary *unitDict = [NSDictionary dictionaryWithObjectsAndKeys:THFont(font - 53),NSFontAttributeName,THWhiteAlplaColor(0.6),NSForegroundColorAttributeName, nil];
          NSRange range = [_humidityView.numberStr rangeOfString:THUnitType_percent];
          NSMutableAttributedString *mutStr = [[NSMutableAttributedString alloc]initWithString: _humidityView.numberStr attributes:numDict];
          
          [mutStr setAttributes:unitDict range:range];
          _humidityView.dateLable.attributedText = mutStr;

      }else
      {
          _humidityView.numberStr = [NSString stringWithFormat:@"%@%@",outStr,THUnitType_percent];
          _humidityView.dateLable.attributedText = [SampuxAttributedString handleUnitString:_humidityView.numberStr unitFont:font - 53 numFont:font unitString:THUnitType_percent];
      }
}


- (void)shutBtnOnHandClick:(SampuxContentBtn *)btn
{
    btn.selected = !btn.selected;
    self.model.powerSwitchSetTime = [SampuxGetCurrentTime getCurrentTimeWithDate];
    if (btn.selected == YES) {
        if (self.addOffViewBtnBlock) {
            self.addOffViewBtnBlock();
        }
        if (self.isConnectOrNot == YES) {
            [GBInterMaster_CommForDeivce requestPowerSwitch:0 withDeviceAccount:self.model.accountStr withPowerSwitch:2];
        }else
        {
            [SVProgressHUD showErrorWithStatus:@"网络未连接，请检查您的网络连接设置"];
        }
        
    }else if (btn.selected == NO)
    {
        if (self.removewOffViewBtnBlock) {
            self.removewOffViewBtnBlock();
        }
        if (self.isConnectOrNot == YES) {
            [GBInterMaster_CommForDeivce requestPowerSwitch:0 withDeviceAccount:self.model.accountStr withPowerSwitch:1];
        }else
        {
            [SVProgressHUD showErrorWithStatus:@"网络未连接，请检查您的网络连接设置"];
        }
//        self.model.powerSwitchSetTime = [SampuxGetCurrentTime getCurrentTimeWithDate];
    }
}

- (void)installBtnClick:(SampuxContentBtn *)btn
{
    NSLog(@"%ld",(long)btn.tag);
    [self.installBtnArr enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
    }];

    _selectedInstallBtn = btn;
    _selectedInstallBtn.userInteractionEnabled = YES;
    SampuxListModel *model = self.model;
    if (btn.tag == 10000) {
        model.heatMode = 1;
    }else if (btn.tag == 10001)
    {
        model.heatMode = 2;
    }else
    {
        model.heatMode = 3;
    }
    self.model = model;
    if (self.isConnectOrNot == YES) {
        [GBInterMaster_CommForDeivce requestHeatMode:0 withDeviceAccount:self.model.accountStr withHeatMode:self.model.heatMode];
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"网络未连接，请检查您的网络连接设置"];
    }
    self.model.heatModeSetTime = [SampuxGetCurrentTime getCurrentTimeWithDate];
}

//- (void)shutBtnClick:(UIButton *)btn
//{
////    [self stateOffViewHide];
//    if (self.removewOffViewBtnBlock) {
//        self.removewOffViewBtnBlock();
//    }
//    [GBInterMaster_CommForDeivce requestPowerSwitch:0 withDeviceAccount:self.model.accountStr withPowerSwitch:1];
//    self.shutBtnOnHand.selected = NO;
//    self.shutBtnOnTime.selected = NO;
//}

//- (void)shutOnHandClick:(SampuxContentBtn *)btn
//{
//    btn.selected = !btn.selected;
//    if (btn.selected == YES) {
//        if (self.addOffViewBtnBlock) {
//            self.addOffViewBtnBlock();
//        }
//        [GBInterMaster_CommForDeivce requestPowerSwitch:0 withDeviceAccount:self.model.accountStr withPowerSwitch:2];
//    }else
//    {
//        if (self.removewOffViewBtnBlock) {
//            self.removewOffViewBtnBlock();
//        }
//        [GBInterMaster_CommForDeivce requestPowerSwitch:0 withDeviceAccount:self.model.accountStr withPowerSwitch:1];
//    }
//}

- (void)shutOnTimeClick:(SampuxContentBtn *)btn
{
    btn.selected = !btn.selected;
    self.model.powerSwitchSetTime = [SampuxGetCurrentTime getCurrentTimeWithDate];

    if (btn.selected == YES) {
        if (self.addOffViewBtnBlock) {
            self.addOffViewBtnBlock();
        }
        if (self.isConnectOrNot == YES) {
            [GBInterMaster_CommForDeivce requestPowerSwitch:0 withDeviceAccount:self.model.accountStr withPowerSwitch:2];
        } else {
            [SVProgressHUD showErrorWithStatus:@"网络未连接，请检查您的网络连接设置"];
        }
    }else
    {
        if (self.removewOffViewBtnBlock) {
            self.removewOffViewBtnBlock();
        }
        if (self.isConnectOrNot == YES) {
            [GBInterMaster_CommForDeivce requestPowerSwitch:0 withDeviceAccount:self.model.accountStr withPowerSwitch:1];

        } else {
            [SVProgressHUD showErrorWithStatus:@"网络未连接，请检查您的网络连接设置"];
        }
    }
}

//- (void)requestSwitch:(NSNotification *)notification
//{
//    id userInfo = [notification userInfo];
//    if(userInfo == nil){
//        return;
//    }
//    NSDictionary * result = userInfo;
//    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
//    int responseCode = [[result objectForKey:@"responseCode"] intValue];
//    NSLog(@"requestSwitch == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
//    if(responseCode == 0x00){
////        [MBProgressHUD showError:@"设置开关机成功"];
//
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"setParameterSuccess" object:nil];
//    }else
//    {
//        [MBProgressHUD showError:@"设置开关机失败"];
//    }
//}

//- (void)requestWorkMode:(NSNotification *)notification
//{
//    id userInfo = [notification userInfo];
//    if(userInfo == nil){
//        return;
//    }
//    NSDictionary * result = userInfo;
//    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
//    int responseCode = [[result objectForKey:@"responseCode"] intValue];
//    NSLog(@" requestWorkMode == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
//    if(responseCode == 0x00){
////        [MBProgressHUD showError:@"设置模式成功"];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"setParameterSuccess" object:nil];
//    }else
//    {
//        [MBProgressHUD showError:@"设置模式失败"];
//    }
//}
//
//- (void)requestHeatMode:(NSNotification *)notification
//{
//    id userInfo = [notification userInfo];
//    if(userInfo == nil){
//        return;
//    }
//    NSDictionary * result = userInfo;
//    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
//    int responseCode = [[result objectForKey:@"responseCode"] intValue];
//    NSLog(@"requestHeatMode == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
//    if(responseCode == 0x00){
////        [MBProgressHUD showError:@"设置加热模式成功"];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"setParameterSuccess" object:nil];
//    }else
//    {
//        [MBProgressHUD showError:@"设置加热模式失败"];
//    }
//}
//
//- (void)setTemp:(NSNotification *)notification
//{
//    id userInfo = [notification userInfo];
//    if(userInfo == nil){
//        return;
//    }
//    NSDictionary * result = userInfo;
//    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
//    int responseCode = [[result objectForKey:@"responseCode"] intValue];
//    NSLog(@"setTemp == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
//    if(responseCode == 0x00){
////        [MBProgressHUD showError:@"设置温度成功"];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"setParameterSuccess" object:nil];
//    }else
//    {
//        [MBProgressHUD showError:@"设置温度失败"];
//    }
//}
- (void)setTitleStr:(NSString *)titleStr
{
    _titleStr = titleStr;
//    _titleLab.text = _titleStr;
}

//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter]removeObserver:self];
//}

@end
